Repository navigation
Facade: OpenRowReader(Async) and CreateEncoder(Stream) streaming entry points - #12
Merged
Merged
Conversation
- ImageCodecs.OpenRowReader / OpenRowReaderAsync sniff a forward-only stream (64 bytes, or a leading ftyp box whole up to 4 KiB, so the choice never depends on how reads arrive), then use the codec's CreateRowReader or a BufferedRowReader whose failures carry the codec's reason (InvalidDataException) or NotSupportedException for an image with no integer projection. Unknown formats throw InvalidDataException(NoCodecMessage), as ImageDecoder.Open does. - ImageCodecs.CreateEncoder(format, Stream, options, leaveOpen) returns StreamEncoderSession / StreamFrameEncoder: the format's session over a pooled 64 KiB chunk writer; WriteRows/Commit write synchronously, WriteRowsAsync/CommitAsync only asynchronously. IsStreaming forwards. - Encode(..., Stream) overloads write through the same chunk writer (same bytes, no whole-file ArrayBufferWriter); docs updated. - Test hooks EncoderSnapshot / ExchangeEncoders; FacadeStreamingTests; AotSmoke/WasmSmoke cover the new entry points; README (en + zh-CN) Streaming subsection. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Unit 1 of the streaming batch: the facade's stream entry points, on top of the Abstractions foundation (84b58f2). No codec package or Abstractions change;
ImageDecoder.csuntouched.What changes
ImageCodecs.OpenRowReader(stream, format, options, leaveOpen, ct)/OpenRowReaderAsync(...)return anIImageRowReader. The stream is wrapped in aStreamInput, and the sniff reads 64 bytes, or a leading ISO-BMFFftypbox whole (up to 4 KiB). The sniff always sees the same bytes whatever the read sizes, so the codec chosen never depends on the feed. Dispatch uses the registry order of the spanFind. A codec withCreateRowReadergets its streaming reader; any other codec gets aBufferedRowReader. Its failures carry the codec's reason asInvalidDataException(viaDecodeFailure), and an image with no integer projection (float, PQ/HLG) throwsNotSupportedException, asDecode(data, format)does. An unknown format throwsInvalidDataException(NoCodecMessage), matchingImageDecoder.Open. The header is read before the method returns. On failure the stream is disposed unlessleaveOpenis set.ImageCodecs.CreateEncoder(format, Stream, options, leaveOpen)returns the new publicStreamEncoderSession(IImageEncoderSession,IAsyncDisposable) withStreamFrameEncoderframes. The format's session writes into an internal pooled chunk writer (StreamBufferWriter, 64 KiB).WriteRows/Commitwrite synchronously, flushing a chunk as soon as it fills.WriteRowsAsync/CommitAsynconly write asynchronously: they encode synchronously, then await the write of the pending bytes. After a large write the buffer shrinks back to one chunk.IsStreamingforwards the inner session's. Commit flushes the stream.Encode(..., Stream)overloads write through the same chunk writer, so the bytes are the same as before and there is no whole-fileArrayBufferWriter. The docs no longer say "buffered, then written in one call" or suggestPipeWriter, and now note that a failed encode may leave a partial file.NoProjectionwas factored out ofDecode(data, format)without changing its messages. New internal test hooks:EncoderSnapshot/ExchangeEncoders.FacadeSmoke(shared with WasmSmoke) gains two checks. A syncOpenRowReaderover a non-seekable stream runs for every sample, and the rows must equalTryDecodeInto. For every built-in encoder,CreateEncoder(Stream)withWriteRowsAsync/CommitAsyncmust produce the bytes ofEncode, andOpenRowReaderAsyncmust return the same rows asTryDecodeInto.Tests (
tests/.../Streaming/FacadeStreamingTests.cs, in the registry collection)RawStreamCodecregistered first: every feed, including async-only, equalsTryDecodeIntowith IsStreaming true; the codec's fallback variant gives IsStreaming false; the reader reads only as far as the rows need.NonStreamingRegistrationgoes throughBufferedRowReaderwith equal rows. Every built-in format, plus WebP lossy/lossless, JBIG2 and JP2 fixtures, at full size, 1/2 scale and a region.ftypbox withavifpast byte 64 gives the AVIF message. Truncated or corrupt PNG throws InvalidDataException under every feed, with the format-prefixed reason. Float PFM throws NotSupportedException.leaveOpenis honoured on success and on failure, and cancellation is checked.CreateEncoder(Stream)bytes equalCreateEncoder(IBufferWriter)for every encoder format, both sync in bands and async-only into a stream whose sync Write/Flush throw. A fake streaming encoder shows bytes reaching the stream before Commit, never more than about one chunk held back; I broke the async flush on purpose and this test then failed. Argument and disposed-state checks.Encode(Stream)bytes are unchanged and the stream is not flushed.OpenRowReaderAsync(async-only ChunkedStream) →CreateEncoder(Png, async-only stream)→WriteRowsAsync/CommitAsync; the re-decoded pixels equalTryDecodeIntoof the source.Validation (Windows, this worktree)
dotnet build SimdCodecs.slnx -c Release: 0 warnings.DOTNET_EnableAVX2=0andDOTNET_EnableHWIntrinsic=0.AotSmoke/run.sh: NativeAOT output identical to the JIT's (402 lines, default and native ISA).WasmSmoke/run.sh: Mono AOT output identical (402 lines).No soak command is needed: no entropy decoder or container parser changed.
Notes for other units: a gain-map JPEG sniffs as plain JPEG on the stream path, because the gain map lies past the sniffed bytes. Its rows are the SDR base, which is also what
TryDecodeIntogives. Each smoke line records whether every encoder session streams (s/b), so that line changes when a codec gains a streaming session, identically under JIT and AOT.🤖 Generated with Claude Code